﻿Imports System.Data.SqlClient

Public Class ProvidersForm

    Dim DefaultRegion As String
    Dim DefaultLA As String


    Private Sub LoadDefaults()
        Job("Loading defaults")

        Dim sql As String
        Dim TheView As DataView

        sql = "select * from Users where User_Name = '" & CurrentUser.Name & "'"

        TheView = LoadDataView(sql)

        If TheView.Table.Rows.Count > 0 Then

            DefaultRegion = TheView(0).Item("Region_Code").ToString
            DefaultLA = TheView(0).Item("LA_Code").ToString
        Else
            DefaultRegion = ""
            DefaultLA = ""
        End If

        TheView = Nothing

    End Sub

    Private Sub SetDefaults()
        If DefaultRegion = "" Then Exit Sub

        RegionBox.SelectedValue = DefaultRegion

        If DefaultLA = "" Then Exit Sub

        LABox.SelectedValue = DefaultLA
    End Sub

    Private Sub ProvidersForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LoadRegionBox()
        LoadLABox()

        LoadDefaults()
        SetDefaults()

    End Sub

    Private Sub LoadRegionBox()
        Job("Loading Regions...")

        RegionBox.DataSource = LoadDataView("Select * from Regions")
        RegionBox.DisplayMember = "Region_Name"
        RegionBox.ValueMember = "Region_Code"
        RegionBox.Refresh()



    End Sub

    Private Sub LoadLABox()

        Job("Loading Local Authorities...")
        Dim sql As String



        sql = "Select * from Local_Authorities where Region_Code = '" & RegionBox.SelectedValue.ToString & "' order by LA_Name"

        LABox.DataSource = LoadDataView(sql)
        LABox.DisplayMember = "LA_Name"
        LABox.ValueMember = "LA_Code"
        LABox.Refresh()



    End Sub


    Private Sub LoadProviderBox()
        Job("Loading Providers...")

        Dim sql As String

        ShowLoadingForm()

        If LABox.SelectedValue IsNot Nothing Then

            sql = "select rel.RID, p.UPIN, p.Provider_Name as 'Provider Name', pt.Provider_Type_Description as 'Type' " & _
                    "from Local_Authorities la, Providers p, Relationships rel, Provider_Types pt " & _
                    "where la.LA_Code = rel.LA_Code " & _
                    "and p.UPIN = rel.UPIN " & _
                    "and p.Provider_type_Code = pt.Provider_Type_Code " & _
                    "and la.LA_Code = '" & LABox.SelectedValue.ToString & "'"

            ProviderListBox.DataSource = LoadDataView(sql)
            ProviderListBox.Refresh()
            ProviderListBox.Columns(0).Visible = False

        End If

        HideLoadingForm()

    End Sub

    Private Sub RegionBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegionBox.SelectedIndexChanged
        LoadLABox()
 
    End Sub

    Private Sub LABox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LABox.SelectedIndexChanged
        LoadProviderBox()

    End Sub

    Private Sub LABox_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LABox.DataSourceChanged
        'first clear the listbox of all data
        ProviderListBox.DataSource = Nothing

    End Sub

    Private Sub ProviderListBox_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ProviderListBox.CellDoubleClick


        ' Check to see if a provider exists in the list, if not do nothing
        If Me.ProviderListBox.CurrentRow.Index = -1 Then

        Else
            'Volumes Form Docking to Main Form
            ShowLoadingForm()

            VolumesForm.MdiParent = MainForm
            VolumesForm.Dock = DockStyle.Fill

            VolumesForm.CurrentProviderName(ProviderListBox.Item(2, ProviderListBox.CurrentRow.Index).Value, _
                                            ProviderListBox.Item(0, ProviderListBox.CurrentRow.Index).Value, _
                                            DirectCast(LABox.SelectedItem, DataRowView).Item("LA_Code"), _
                                            ProviderListBox.Item(1, ProviderListBox.CurrentRow.Index).Value)


            VolumesForm.Show()
            HideLoadingForm()

        End If


    End Sub

   
  
    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        Search_Form.WhoSentForMe = "Providers"
        Search_Form.ShowDialog()

    End Sub

    Private Sub DefaultBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DefaultBut.Click
        'set the current selection as the default

        Dim sql As String

        sql = "Update Users set Region_Code = '" & RegionBox.SelectedValue.ToString & "', LA_Code = '" & LABox.SelectedValue.ToString & "' " & _
              "where User_name = '" & CurrentUser.Name & "'"

        RunNonQuery(sql)

        DefaultRegion = RegionBox.SelectedValue.ToString
        DefaultLA = LABox.SelectedValue.ToString

        MessageBox.Show("The defaults have been set", "Default set", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub ProviderListBox_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ProviderListBox.CellContentClick

    End Sub
End Class